package com.wuliu.dao.impl;


import com.wuliu.dao.OrdersDAO;
import com.wuliu.model.Orders;
import com.wuliu.util.Jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class OrdersDAOImpl implements OrdersDAO {
    @Override
    public void add(Orders orders) {
        try {
            Jdbc jdbc = new Jdbc();
            Connection connection = jdbc.connection();
            String sql="insert into orders values(null,?,?,?,?,?,?,?,?,?,?,?,?)";
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setString(1,orders.getSenderName());
            statement.setString(2,orders.getSenderAddress());
            statement.setString(3,orders.getSenderPhone());
            statement.setDouble(4,orders.getWeight());
            statement.setString(5,orders.getRecipientName());
            statement.setString(6,orders.getRecipientAddress());
            statement.setString(7,orders.getRecipientPhone());
            statement.setString(8,orders.getOther());
            statement.setString(9,orders.getShoppingMethod());
            statement.setString(10,orders.getPayment());
            statement.setString(11,orders.getClaimGoods());
            statement.setString(12,orders.getPackagingService());
            statement.execute();
            statement.close();
            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<Orders> findAll() {
        List<Orders> ordersList=new ArrayList<>();
        try {
            Jdbc jdbc =new Jdbc();
            Connection connection=jdbc.connection();
            String sql="select * from orders";
            PreparedStatement statement=connection.prepareStatement(sql);
            ResultSet set=statement.executeQuery();
            while (set.next()){
                int id=set.getInt("id");
                String sender_name=set.getString("sender_name");
                String sender_address=set.getString("sender_address");
                String sender_phone=set.getString("sender_phone");
                double weight=set.getDouble("weight");
                String recipient_name=set.getString("recipient_name");
                String recipient_address=set.getString("recipient_address");
                String recipient_phone=set.getString("recipient_phone");
                String other=set.getString("other");
                String shipping_method=set.getString("shipping_method");
                String payment =set.getString("payment");
                String claim_goods=set.getString("claim_goods");
                String packaging_service=set.getString("packaging_service");
                Orders orders=new Orders(id,sender_name,sender_address,sender_phone,weight,recipient_name,recipient_address,recipient_phone,other,shipping_method,payment,claim_goods,packaging_service);
                ordersList.add(orders);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ordersList;
    }
}
